package com.yanqu.road.dao.impl.channelcenter.washe;

import com.yanqu.road.dao.cache.SqlCacheParam;
import com.yanqu.road.dao.impl.TempNoPoolCacheDao;
import com.yanqu.road.entity.activity.washe.config.WasheSeasonConfig;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class TempWasheSeasonConfigDaoImpl extends TempNoPoolCacheDao {

    public TempWasheSeasonConfigDaoImpl(String dbParam) {
        super(dbParam);
    }

    private static String getWasheSeasonTimeConfigSql = "select * from `t_n_washepvp_seasontime` where `activityId`=?";

    public Map<Integer, WasheSeasonConfig> getWasheSeasonTimeConfigMap(int activityId, int channelActivityTimeZone) {

        SqlCacheParam sqlCacheParam = getCacheParam(getWasheSeasonTimeConfigSql);
        sqlCacheParam.addParam(Integer.class, activityId);
        sqlCacheParam.addOtherParam(channelActivityTimeZone);
        // 先取缓存
        Object cacheResult = getCacheResult(sqlCacheParam);
        if (cacheResult != null) {
            return (Map<Integer, WasheSeasonConfig>) cacheResult;
        }

        boolean hasThemeDesc = true;

        Map<Integer, WasheSeasonConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            call = createCall(sqlCacheParam);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                WasheSeasonConfig washeSeasonConfig = new WasheSeasonConfig();
                washeSeasonConfig.setActivityId(rs.getInt("activityId"));
                washeSeasonConfig.setSeasonId(rs.getInt("seasonId"));
                washeSeasonConfig.setBeginShowTimeStr(rs.getString("beginShowTime"));
                washeSeasonConfig.setBeginTimeStr(rs.getString("beginTime"));
                washeSeasonConfig.setEndTimeStr(rs.getString("endTime"));
                washeSeasonConfig.setEndShowTimeStr(rs.getString("endShowTime"));
                washeSeasonConfig.setIntegralBeginTimeStr(rs.getString("integralBeginTime"));
                washeSeasonConfig.setIntegralEndTimeStr(rs.getString("integralEndTime"));
                washeSeasonConfig.setPeakednessBeginTimeStr(rs.getString("peakednessBeginTime"));
                washeSeasonConfig.setPeakednessEndTimeStr(rs.getString("peakednessEndTime"));
                washeSeasonConfig.setJinBiaoSwitch(rs.getInt("switch"));
                washeSeasonConfig.setTaskItemId(rs.getInt("taskItemId"));
                washeSeasonConfig.setTheme(rs.getString("theme"));

                if (hasThemeDesc) {
                    try {
                        washeSeasonConfig.setThemeDesc(rs.getString("themeDesc"));
                    } catch (Exception e) {
                        hasThemeDesc = false;
                    }
                }

                // init
                washeSeasonConfig.init(channelActivityTimeZone);

                map.put(washeSeasonConfig.getSeasonId(), washeSeasonConfig);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            closeConnection(call, rs);
        }
        // 添加缓存
        addCacheResult(sqlCacheParam, map);
        return map;
    }

}
